﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
#pragma warning disable CS1591
namespace Core.SimpleTemp.Common.Filters
{

    public class HttpGlobalExceptionFilter : IExceptionFilter
    {



        private readonly IHostingEnvironment _env;
        private ILogger<HttpGlobalExceptionFilter> _logger;
        private IHttpContextAccessor _httpContextAccessor;
        public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger<HttpGlobalExceptionFilter> logger, IHttpContextAccessor httpContextAccessor)
        {
            _env = env;
            _logger = logger;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            context.Result = ExceptionHelp.CreateErrorResponse(context.Exception,_env.IsDevelopment(), _httpContextAccessor.HttpContext.TraceIdentifier);
            _logger.LogError(context.Exception, context.Exception.Message);
            context.ExceptionHandled = true;
        }

       
    }
}
